/**
 * ChatMessageCustomer.java
 */
package com.tobeface.sns.infrastructure.amqp;

import java.io.IOException;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.kissme.core.helper.JsonHelper;
import com.rabbitmq.client.Channel;
import com.tobeface.sns.domain.message.ChatMessage;
import com.tobeface.sns.infrastructure.amqp.config.AmqpChatClientConfiguration;

/**
 * @author Dylan
 * @time 2012-8-2
 */
@Component
public class ChatMessageProducer {

	@Resource
	private AmqpChatClientConfiguration configuration;

	public ChatMessageProducer(){
	}

	public void sendMessage(ChatMessage message) {
		try {
			String msg = JsonHelper.toJsonString(message);
			Channel channel = configuration.getChannel();
			channel.basicPublish(configuration.getExchangeName(), configuration.key(), null, msg.getBytes());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			configuration.destroy();
		}
	}

}
